package com.lins.framework.utils.tip;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

/**
 * Created by Admin on 2017/5/15.
 */

public class SimpleMDDialog extends DialogFragment {

    public static SimpleMDDialog newInstance(String title, String message, String negative, String positive) {

        Bundle args = new Bundle();

        SimpleMDDialog fragment = new SimpleMDDialog();
        args.putString("alert-title", title);
        args.putString("alert-message", message);
        args.putString("alert-negative", negative);
        args.putString("alert-positive", positive);
        args.putBoolean("alert-has-negative", true);
        fragment.setArguments(args);
        return fragment;
    }

    public static SimpleMDDialog newInstance(String title, String message, String positive) {
        Bundle args = new Bundle();

        SimpleMDDialog fragment = new SimpleMDDialog();
        args.putString("alert-title", title);
        args.putString("alert-message", message);
        args.putString("alert-positive", positive);
        args.putBoolean("alert-has-negative", false);
        fragment.setArguments(args);
        return fragment;
    }

    private String getTitle() {
        return getArguments().getString("alert-title");
    }

    private String getMessage() {
        return getArguments().getString("alert-message");
    }

    private String getNegative() {
        return getArguments().getString("alert-negative");
    }

    private String getPositive() {
        return getArguments().getString("alert-positive");
    }

    private boolean hasNegative() {
        return getArguments().getBoolean("alert-has-negative");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getTitle());
        builder.setMessage(getMessage());
        builder.setPositiveButton(getPositive(), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (hasNegative())
                    mOnCancelAndOkBtnClickListener.onOkClickListener(dialogInterface);
                else
                    mOnOkBtnClickListener.onOkClickListener(dialogInterface);

            }
        });
        if (hasNegative()) {
            builder.setNegativeButton(getNegative(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mOnCancelAndOkBtnClickListener.onCancelClickListener(dialogInterface);
                }
            });
        }

        return builder.create();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    private OnCancelAndOkBtnClickListener mOnCancelAndOkBtnClickListener;
    private OnOkBtnClickListener mOnOkBtnClickListener;

    public interface OnCancelAndOkBtnClickListener {
        void onCancelClickListener(DialogInterface dialog);

        void onOkClickListener(DialogInterface dialog);
    }

    public interface OnOkBtnClickListener {
        void onOkClickListener(DialogInterface dialog);
    }

    public void setOnCancelAndOkBtnClickListener(OnCancelAndOkBtnClickListener mOnCancelAndOkBtnClickListener) {
        this.mOnCancelAndOkBtnClickListener = mOnCancelAndOkBtnClickListener;
    }

    public void setOnOkBtnClickListener(OnOkBtnClickListener mOnOkBtnClickListener) {
        this.mOnOkBtnClickListener = mOnOkBtnClickListener;
    }


}
